<template>
	<view class="content">
		<scroll-view scroll-y class="left-aside">
			<view v-for="(item,key) in flist" :key="item.id" class="f-item b-b" :class="{active: item.id === currentId}" @click="tabtap(key,item)">
				{{item.title}}
			</view>
		</scroll-view>
		<scroll-view scroll-with-animation scroll-y class="right-aside" @scroll="asideScroll" :scroll-top="tabScrollTop">
			<view v-for="item in slist" :key="item.id" class="s-list" :id="'main-'+item.id">
				<text class="s-item">{{item.title}}</text>
				<view class="t-list">
					<view @click="navToList(titem.id)" class="t-item" v-for="titem in item.children" :key="titem.id">
						<xy-image :src="titem.cover"></xy-image>
						<text>{{titem.title}}</text>
					</view>
				</view>
			</view>
		</scroll-view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				sizeCalcState: false,
				tabScrollTop: 0,
				currentId: 1,
				flist: [],
				slist: [],
				tlist: [],
			}
		},
		onLoad(){
			this.loadData();
		},
		async onPullDownRefresh() {
			await this.loadData();
			uni.stopPullDownRefresh();
			setTimeout(function () {
				uni.stopPullDownRefresh();
			}, 5000);
		},
		onShareAppMessage(res) {
			if (res.from === 'button') {// 来自页面内分享按钮
				console.log(res.target)
			}
			return {
				title: this.$store.state.app.siteInfo.title,
				path: '/pages/ishop/icate/trees'
			}
		},
		methods: {
			async loadData(){
				let res = await this.util.request({
					url: '/v1/ishop/icate/trees',
					method: 'get',
				});
				if (res.code == 200) {
					this.flist = res.data.dataList;
					this.currentId = res.data.dataList[0]['id'];
					this.flist.forEach(item => {
						if (item.children) {
							item.children.forEach(item1 => {
								if (item1.children) {
									this.slist = this.slist.concat(item1.children);
								}
							});
						}
					});
				}
			},
			//一级分类点击
			tabtap(key,item){
				if(!this.sizeCalcState){
					this.calcSize();
				}
				this.currentId = item.id;
				if (this.flist[key].children) {
					this.flist[key].children.forEach(item1 => {
						let index = this.slist.findIndex(sitem=>sitem.pid === item1.id);
						if (index > 0) {
							this.tabScrollTop = this.slist[index].top;
							return true;
						}
					});
				}
			},
			//右侧栏滚动
			asideScroll(e){
				if(!this.sizeCalcState){
					this.calcSize();
				}
				let scrollTop = e.detail.scrollTop;
				let tabs = this.slist.filter(item=>item.top <= scrollTop).reverse();
				// console.log(tabs)
				if(tabs.length > 0){
					this.flist.forEach(item => {
						if (item.children) {
							item.children.forEach(item1 => {
								if (item1.id == tabs[0].pid) {
									this.currentId = item.id;
								}
							});
						}
					});
				}
			},
			// 计算右侧栏每个tab的高度等信息
			calcSize(){
				let h = 0;
				this.slist.forEach(item=>{
					let view = uni.createSelectorQuery().select("#main-" + item.id);
					view.fields({
						size: true
					}, data => {
						item.top = h;
						h += data.height;
						item.bottom = h;
					}).exec();
				})
				this.sizeCalcState = true;
			},
			navToList(icateId){
				uni.navigateTo({
					url: `/pages/ishop/item/lists?icateId=${icateId}`
				})
			}
		}
	}
</script>

<style lang='scss'>
	page,
	.content {
		height: 100%;
		background-color: #f8f8f8;
	}

	.content {
		display: flex;
	}
	.left-aside {
		flex-shrink: 0;
		width: 100px;
		height: 100%;
		background-color: #fff;
	}
	.f-item {
		display: flex;
		align-items: center;
		justify-content: center;
		width: 100%;
		height: 50px;
		font-size: 13px;
		color: $font-color-base;
		position: relative;
		&.active{
			color: $uni-color-primary;
			background: #f8f8f8;
			&:before{
				content: '';
				position: absolute;
				left: 0;
				top: 50%;
				transform: translateY(-50%);
				height: 25px;
				width: 8rpx;
				background-color: $uni-color-primary;
				border-radius: 0 4px 4px 0;
				opacity: .8;
			}
		}
	}

	.right-aside{
		flex: 1;
		overflow: hidden;
		padding-left: 20rpx;
	}
	.s-item{
		display: flex;
		align-items: center;
		height: 35px;
		padding-top: 4px;
		font-size: 12px;
		color: $font-color-dark;
	}
	.t-list{
		display: flex;
		flex-wrap: wrap;
		width: 100%;
		background: #fff;
		padding-top: 12rpx;
		&:after{
			content: '';
			flex: 99;
			height: 0;
		}
	}
	.t-item{
		flex-shrink: 0;
		display: flex;
		justify-content: center;
		align-items: center;
		flex-direction: column;
		width: 33%;
		font-size: 12px;
		color: #666;
		padding-bottom: 10px;
		image{
			width: 140rpx;
			height: 140rpx;
		}
	}
</style>
